package common

import (
	"fmt"
	"net/url"
	"github.com/jinzhu/gorm"
	_"github.com/go-sql-driver/mysql"
	"OceanLearn/model"
)

var DB  *gorm.DB

func InitDb() *gorm.DB {
	driverName := "mysql"
	//host := "localhost"
	host := "39.99.211.224"
	port := "3306"
	database := "gin"
	username := "gin"
	password := "123"
	charset := "utf8"
	loc := "Asia/Shanghai"
	args := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true&loc=%s",
		username,
		password,
		host,
		port,
		database,
		charset,
		url.QueryEscape(loc))
	fmt.Printf(args)
	db, err := gorm.Open(driverName, args)
	if err != nil {
		panic("fail to connect database, err: " + err.Error())
	}
	db.AutoMigrate(&model.User{})

	DB = db
	return db
}

func GetDB() *gorm.DB {
	return DB
}